﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO = Microsoft.MSE.Catalog.Contracts;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace Microsoft.MSE.Catalog.Module.Data
{
    public class MSEType
    {
        public MSEType()
        { }

        public static Guid CreateMSEType(DTO.MSEType mseType, SqlTransaction txn, SqlConnection conn)
        {
            Guid ID;
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[CreateType]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);
                    SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);
                    SqlParameter OwnerNameParam = new SqlParameter("@OwnerName", SqlDbType.NText);
                    SqlParameter OwnerContactParam = new SqlParameter("@OwnerContact", SqlDbType.NChar, 10);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Direction = ParameterDirection.Output;
                    NameParam.Value = mseType.Name;
                    if (mseType.OwnerName != null)
                    {
                        OwnerNameParam.Value = mseType.OwnerName;
                    }
                    else
                    {
                        OwnerNameParam.Value = DBNull.Value;
                    }
                    if (mseType.OwnerContact != null)
                    {
                        OwnerContactParam.Value = mseType.OwnerContact;
                    }
                    else
                    {
                        OwnerContactParam.Value = DBNull.Value;
                    }
                    if (mseType.Description != null)
                    {
                        DescriptionParam.Value = mseType.Description;
                    }
                    else
                    {
                        DescriptionParam.Value = DBNull.Value;
                    }



                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(OwnerNameParam);
                    cmd.Parameters.Add(OwnerContactParam);
                    cmd.Parameters.Add(DescriptionParam);


                    cmd.ExecuteNonQuery();
                    ID = (Guid)IDParam.Value;
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return ID;
        }

        public static DTO.MSEType UpdateMSEType(DTO.MSEType mseType, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[UpdateType]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);
                    SqlParameter OwnerNameParam = new SqlParameter("@OwnerName", SqlDbType.NVarChar, 200);
                    SqlParameter OwnerContactParam = new SqlParameter("@OwnerContact", SqlDbType.NChar, 200);
                    SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = mseType.ID;
                    NameParam.Value = mseType.Name;
                    if (mseType.OwnerName != null)
                    {
                        OwnerNameParam.Value = mseType.OwnerName;
                    }
                    else
                    {
                        OwnerNameParam.Value = DBNull.Value;
                    }
                    if (mseType.OwnerContact != null)
                    {
                        OwnerContactParam.Value = mseType.OwnerContact;
                    }
                    else
                    {
                        OwnerContactParam.Value = DBNull.Value;
                    }
                    if (mseType.Description != null)
                    {
                        DescriptionParam.Value = mseType.Description;
                    }
                    else
                    {
                        DescriptionParam.Value = DBNull.Value;
                    }

                    //


                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(OwnerNameParam);
                    cmd.Parameters.Add(OwnerContactParam);
                    cmd.Parameters.Add(DescriptionParam);

                    cmd.ExecuteNonQuery();
                }
            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }


            return mseType;
        }

        public static void DeleteMSEType(Guid ID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[DeleteType]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    cmd.Parameters.Add(IDParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

        }

        public static DTO.MSEType GetMSEType(Guid ID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            DTO.MSEType type = new DTO.MSEType();
            const string sprocname = "[ServiceEngine].[GetType]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    cmd.Parameters.Add(IDParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            reader.Read();
                            type.ID = reader.GetGuid(0);
                            type.Name = reader.GetString(1);

                            if (reader.IsDBNull(2))
                            {
                                type.OwnerName = null;
                            }
                            else
                            {
                                type.OwnerName = reader.GetString(2);
                            }
                            if (reader.IsDBNull(3))
                            {
                                type.OwnerContact = null;
                            }
                            else
                            {
                                type.OwnerContact = reader.GetString(3);
                            }
                            if (reader.IsDBNull(4))
                            {
                                type.Description = null;
                            }
                            else
                            {
                                type.Description = reader.GetString(4);
                            }                                                       

                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }


            return type;

        }

        public static DTO.MSETypeList ListMSETypes(SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            DTO.MSETypeList list = new DTO.MSETypeList();
            const string sprocname = "[ServiceEngine].[ListTypes]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            list.MSETypes = new List<DTO.MSETypeListElement>();
                            while (reader.Read())
                            {
                                DTO.MSETypeListElement element = new DTO.MSETypeListElement();
                                element.ID = reader.GetGuid(0);
                                element.Name = reader.GetString(1);
                                if (reader.IsDBNull(2))
                                {
                                    element.Description = null;
                                }
                                else
                                {
                                    element.Description = reader.GetString(2);
                                }
                                list.MSETypes.Add(element);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return list;

        }

        public static DTO.DataEntityList ListDataEntitiesForMSEType(Guid mseTypeID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            DTO.DataEntityList list = new DTO.DataEntityList();
            const string sprocname = "[ServiceEngine].[ListDataEntitiesForType]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = mseTypeID;
                    cmd.Parameters.Add(IDParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            list.DataEntities = new List<DTO.DataEntityListElement>();
                            while (reader.Read())
                            {
                                DTO.DataEntityListElement element = new DTO.DataEntityListElement();
                                element.ID = reader.GetGuid(0);
                                element.Name = reader.GetString(1);
                                if (reader.IsDBNull(2))
                                {
                                    element.Description = null;
                                }
                                else
                                {
                                    element.Description = reader.GetString(2);
                                }
                                list.DataEntities.Add(element);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return list;

        }

        public static List<DTO.MSETypeListElement> ListMSETypesByGroup(Guid entityGroupID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            List<DTO.MSETypeListElement> list = new List<DTO.MSETypeListElement>();
            const string sprocname = "[ServiceEngine].[ListEntitiesByGroup]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@entityGroupID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = entityGroupID;
                    cmd.Parameters.Add(IDParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            list = new List<DTO.MSETypeListElement>();
                            while (reader.Read())
                            {
                                DTO.MSETypeListElement element = new DTO.MSETypeListElement();
                                element.ID = reader.GetGuid(0);
                                element.Name = reader.GetString(1);
                                if (reader.IsDBNull(2))
                                {
                                    element.Description = null;
                                }
                                else
                                {
                                    element.Description = reader.GetString(2);
                                }

                                list.Add(element);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return list;

        }

        public static void AssignDataEntityToMSEType(Guid mseTypeID, Guid dataEntityID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[AssignDataEntityToType]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter TypeIDParam = new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier);
                    SqlParameter DataEntityIDParam = new SqlParameter("@DataEntityID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    TypeIDParam.Value = mseTypeID;
                    DataEntityIDParam.Value = dataEntityID;

                    cmd.Parameters.Add(TypeIDParam);
                    cmd.Parameters.Add(DataEntityIDParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }
        }

        public static void RemoveDataEntityFromMSEType(Guid mseTypeID, Guid dataEntityID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[RemoveDataEntityFromType]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter TypeIDParam = new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier);
                    SqlParameter DataEntityIDParam = new SqlParameter("@DataEntityID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    TypeIDParam.Value = mseTypeID;
                    DataEntityIDParam.Value = dataEntityID;

                    cmd.Parameters.Add(TypeIDParam);
                    cmd.Parameters.Add(DataEntityIDParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

        }

    }
}
